package com.pocketsweet.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_webview)
public class WebViews extends BaseActivity {


    @ViewInject(R.id.webViews)
    private WebView webViews;

    private LoadingDailog mLoadingDialog;
    private HeaderLayout header;
    private String url;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");
        setHeaderFunction();
        initWebView();
    }

    private void initWebView() {
        mLoadingDialog = ToolKits.createLoadingDialog(WebViews.this,
                "登陆中...");
        mLoadingDialog.setText("加载中");
        WebSettings settings = webViews.getSettings();
        settings.setJavaScriptEnabled(true);
        webViews.loadUrl(url);
        webViews.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        webViews.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                if (newProgress == 100) {
                    // 网页加载完成
                    if (mLoadingDialog.isShowing() && !WebViews.this.isFinishing())
                        mLoadingDialog.dismiss();
                    webViews.setVisibility(View.VISIBLE);
                } else {
                    // 加载中
                    mLoadingDialog.show();

                }

            }
        });

    }

    public void setHeaderFunction() {
        header = (HeaderLayout) findViewById(R.id.mainHeader);
        header.init(HeaderStyle.IMG_TITLE_TEXT);
        if (!StringUtils.isEmpty(title)) {
            header.setMiddleText(title);
        }
        header.setRightText("");
        header.setLeftContainerClickListener(new onLeftContainerListener() {

            @Override
            public void onClick() {
                WebViews.this.finish();
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
}
